// formPackage/pages/appointment/myAppointment/myAppointment.js
import { sendApplyAgentCodeAction } from "../../../../service/config"
import { createNewBooking } from "../../../../service/book_service"
import debounce from "../../../../tools/debounce"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showAppointmentPop: false,
    showSuccessPop:false,
    minDate: new Date().getTime(),
    maxDate: new Date(2050, 12, 31).getTime(),
    timePopType: "",
    formData: {
      code: "",
      phone: "",
      num: "1",//到访人数
      houseNumber: "",
      houseName: "",
      houseType: 1,
      name: "",
      orderTime: "",//预约时间
    },
    couponCodeBtnStatus: false,
    couponCodeBtnTitle: "获取验证码",
    snsMsgWait: 60,//倒计时
    isShowCode: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.houseName){
      this.setData({
        'formData.houseName':options.houseName,
        'formData.houseNumber':options.houseNumber,
      })
    }
    if (app.globalData.name && app.globalData.phone) {
      console.log("phone=:" + app.globalData.phone);
      this.setData({
        'formData.phone': app.globalData.phone,
        'formData.name': app.globalData.name
      })
    }
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    if (app.globalData.articleHouse.name) {
      this.setData({
        'formData.houseNumber': app.globalData.articleHouse.houseNumber,
        'formData.houseName': app.globalData.articleHouse.name
      })
      app.globalData.articleHouse = {}
    }
  },
  //单项输入绑定:
  inputCodeChange(e) {
    let detail = e.detail;
    let changeValue = `formData.${detail.key}`;
    this.setData({
      [changeValue]: detail.value
    })
  },
  bindPhoneInputValue(e) {
    this.setData({
      'formData.phone': e.detail.value
    })
  },
  //单项选择弹出:
  singleSelectPop(e) {
    // console.log("JJLog:e:" + JSON.stringify(e));
    let type = e.currentTarget.dataset.type;
    if (type == "time") {
      //选择预约时间:
      this.setData({
        showAppointmentPop: true,
        timePopType: "time"
      })
    } else {
      //选择预约楼盘:
      wx.navigateTo({
        url: '/formPackage/pages/article/selectArticleHouse/selectArticleHouse',
      })
    }
  },


  //选择预约时间:
  appointmentTimeSure(e) {
    console.log("JJLog:交付时间回调:" + JSON.stringify(e));
    let timeStr = e.detail.timeStr;
    this.setData({
      showAppointmentPop: false,
      'formData.orderTime': timeStr
    })
  },
  //消失
  timePopHidden() {
    this.setData({
      showAppointmentPop: false
    })
  },
  /** 发送验证码 */
  clickSendCode() {
    if (this.data.couponCodeBtnStatus) {
      return;
    }
    let that = this;
    //点击了发送按钮，就先禁用掉这个按钮点击
    this.setData({
      couponCodeBtnStatus: true,
    })
    //调用发送验证码接口:
    let phone = this.data.formData.phone;
    let warn = null;
    if (phone == '') {
      warn = "号码不能为空";
    } else if (phone.trim().length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(phone)) {
      warn = "手机号格式不正确";
    } else {
      sendApplyAgentCodeAction(phone).then((data) => {
        //当手机号正确的时候提示用户短信验证码已经发送
        wx.showToast({
          title: '短信验证码已发送',
          icon: 'none',
          duration: 2000
        });
        let inter = setInterval(function () {
          this.setData({
            couponCodeBtnStatus: true,
            couponCodeBtnTitle: this.data.snsMsgWait + 's后重发',
            snsMsgWait: this.data.snsMsgWait - 1
          });
          if (this.data.snsMsgWait < 0) {
            clearInterval(inter)
            this.setData({
              couponCodeBtnTitle: '重新发送',
              snsMsgWait: 60,
              couponCodeBtnStatus: false
            });
          }
        }.bind(this), 1000);

      }).
        catch((err) => {
          that.setData({
            couponCodeBtnStatus: false
          })
          wx.showToast({
            icon: 'none',
            title: err,
          })
        })


    }
    if (warn != null) {
      wx.showModal({
        title: '提示',
        content: warn
      })
      that.setData({
        couponCodeBtnStatus: false
      })
      return;
    }


  },

  //提交预约:
  submitAppointmentAction() {
    let data = this.data.formData;
    if (!data.houseName) {
      wx.showToast({
        title: '请选择预约楼盘',
        icon: "none"
      })
      return;
    }
    if (!data.orderTime) {
      wx.showToast({
        title: '请选择预约时间',
        icon: "none"
      })
      return;
    }
    if (!data.name) {
      wx.showToast({
        title: '请输入您的姓名',
        icon: "none"
      })
      return;
    }
    if (!data.phone) {
      wx.showToast({
        title: '请输入您的手机号',
        icon: "none"
      })
      return;
    }
    if (!data.code) {
      wx.showToast({
        title: '请输入验证码',
        icon: "none"
      })
      return;
    }
    if (!data.num) {
      wx.showToast({
        title: '请输入到访人数',
        icon: "none"
      })
      return;
    }
    console.log("JJLog:提交数据:" + JSON.stringify(this.data.formData));
    debounce(this.createNewBooking);
  },
  createNewBooking() {
    wx.showLoading();
    createNewBooking(this.data.formData).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
       this.setData({
         showSuccessPop:true
       })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "提交失败",
        icon: "none"
      })
    })
  },
  successPopSure(){
    this.setData({
      showSuccessPop: false
    })
    wx.navigateBack({
      delta: 0,
    })
  },




})